﻿Imports System.Web
Imports System.Web.Services
Imports Newtonsoft.Json
Imports System.Reflection
Public Class hndSearch
    Implements System.Web.IHttpHandler
    Private strhelp As New Helpers.Strings
    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            Dim op As String = context.Request("op").Trim 'METODO DA RICHIAMARE
            Dim Params As String() = (From r As String In context.Request.Form.Keys Where r <> "op" Order By r _
                          Select context.Request.Form(r).Trim).ToArray
            If Params.Count = 0 Then
                Params = (From r As String In context.Request.QueryString.Keys Where r <> "op" And r <> "_" _
                          And r <> "callback" Order By r Ascending Select context.Request.QueryString(r).Trim).ToArray
            End If
            Dim t As Type = Me.GetType
            Dim m As MethodInfo = t.GetMethod(op)
            Dim result As String = String.Empty
            If m IsNot Nothing Then
                result = m.Invoke(Me, Params)
            End If
            context.Response.Write(result)
        Catch ex As Exception
            context.Response.Write("0")
        End Try
    End Sub
    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
    Public Function GetComuni(ByVal limit As String, ByVal query As String) As String
        Dim comuniList As IList = Nothing
        If limit > 0 Then
            comuniList = (From c In DAL.Context.Comune Where c.Comune.ToLower.StartsWith(query) Select _
                         idcomune = c.ID, comune = c.Comune & " (" & c.Provincia.Sigla & ")").Take(limit).ToList
        End If
        Return JsonConvert.SerializeObject(comuniList)
    End Function
    Public Function GetAnnunci(ByVal comune As String, ByVal contratto As String, ByVal immobile As String, ByVal orderby As String, ByVal prezzoA As String, ByVal prezzoDa As String, ByVal skipLimit As String, ByVal takeLimit As String) As String
        Dim idcomune As Integer = 0
        Dim idtipocontratto As Integer = 0
        Dim idtipoimmobile As Integer = 0
        Dim priceFrom As Double = 0
        Dim priceTo As Double = 0
        Dim annunci As IEnumerable(Of Annuncio) = DAL.Context.Annuncio.Where(Function(a) a.DataScadenza >= Date.Now Or a.DataScadenza Is Nothing)
        If Not String.IsNullOrEmpty(comune) AndAlso Integer.TryParse(comune, idcomune) Then
            If idcomune <> 0 Then annunci = annunci.Where(Function(a) a.IDComune = idcomune)
        End If
        If Not String.IsNullOrEmpty(contratto) AndAlso Integer.TryParse(contratto, idtipocontratto) Then
            If idtipocontratto <> 0 Then annunci = annunci.Where(Function(a) a.IDTipoContratto = idtipocontratto)
        End If
        If Not String.IsNullOrEmpty(immobile) AndAlso Integer.TryParse(immobile, idtipoimmobile) Then
            If idtipoimmobile <> 0 Then annunci = annunci.Where(Function(a) a.IDTipoImmobile = idtipoimmobile)
        End If
        If Not String.IsNullOrEmpty(prezzoDa) AndAlso Double.TryParse(prezzoDa, priceFrom) Then
            If priceFrom <> 0 Then annunci = annunci.Where(Function(a) a.Prezzo >= priceFrom)
        End If
        If Not String.IsNullOrEmpty(prezzoA) AndAlso Double.TryParse(prezzoA, priceTo) Then
            If priceTo <> 0 And priceTo <> 300000 Then annunci = annunci.Where(Function(a) a.Prezzo <= priceTo)
        End If
        If Not String.IsNullOrEmpty(orderby) Then
            Select Case orderby
                Case "data_asc"
                    annunci = annunci.OrderBy(Function(a) a.DataPubblicazione)
                Case "data_desc"
                    annunci = annunci.OrderByDescending(Function(a) a.DataPubblicazione)
                Case "prezzo_asc"
                    annunci = annunci.OrderBy(Function(a) a.Prezzo)
                Case "prezzo_desc"
                    annunci = annunci.OrderByDescending(Function(a) a.Prezzo)
                Case "superficie_asc"
                    annunci = annunci.OrderBy(Function(a) a.MQ)
                Case "superficie_desc"
                    annunci = annunci.OrderByDescending(Function(a) a.MQ)
                Case "locali_asc"
                    annunci = annunci.OrderBy(Function(a) a.Vani)
                Case "locali_desc"
                    annunci = annunci.OrderByDescending(Function(a) a.Vani)
            End Select
        End If
        Dim annunciList As List(Of AnnuncioDTO) = (From a In annunci Select New AnnuncioDTO( _
                        a.ID, a.Titolo, strhelp.CleanHtml(If(a.Descrizione.Length > 150, a.Descrizione.Substring(0, 150), a.Descrizione)), strhelp.NullToStr(a.Condizione.Descrizione), a.TipoContratto.Descrizione, _
                        a.TipoImmobile.Descrizione, a.Comune.Comune & " (" & a.Comune.Provincia.Sigla & ")", _
                        a.Utente.Cognome & " " & a.Utente.Nome.Substring(0, 1) & ".", strhelp.NullToStr(a.Indirizzo), If(a.CAP, 0), a.DataPubblicazione, _
                        If(a.MQ, 0), If(a.Vani, 0), If(a.Bagni, 0), If(a.AnnoFabbricazione, 0), If(a.Termoautonomo, False), If(a.Piano, 0), _
                        If(a.Note, String.Empty), If(a.Prezzo, 0), If(a.Riferimento, String.Empty))).Skip(skipLimit).Take(takeLimit).ToList
        Return JsonConvert.SerializeObject(annunciList)
    End Function
End Class